﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ComponentModel;

namespace FileRenamer
{
    public class SyncList<T> : System.ComponentModel.BindingList<T>
    {

        private System.ComponentModel.ISynchronizeInvoke _SyncObject;
        private System.Action<System.ComponentModel.ListChangedEventArgs> _FireEventAction;

        public SyncList()
            : this(null)
        {
        }

        public SyncList(System.ComponentModel.ISynchronizeInvoke syncObject)
        {

            _SyncObject = syncObject;
            _FireEventAction = FireEvent;
        }

        public void SetSynchronise(System.ComponentModel.ISynchronizeInvoke syncObject)
        {
             _SyncObject = syncObject;
        }

        protected override void OnListChanged(System.ComponentModel.ListChangedEventArgs args)
        {
            if (_SyncObject == null)
            {
                FireEvent(args);
            }
            else
            {
                _SyncObject.Invoke(_FireEventAction, new object[] { args });
            }
        }

        private void FireEvent(System.ComponentModel.ListChangedEventArgs args)
        {
            base.OnListChanged(args);
        }
    }

    public class ActionItems : SyncList<ActionItem>
    {
        public int GetItemCount(ActionItem.ActionType action)
        {
            int count;

            count = (from actionItem in this where actionItem.Action == action select actionItem).Count();

            return count;
        }

        public Int64 GetFileSetSize()
        {
            Int64 length = 0;

            foreach (ActionItem actionItem in this)
            {
                if (File.Exists(actionItem.Source) && (actionItem.Action == ActionItem.ActionType.MOVE || actionItem.Action == ActionItem.ActionType.COPY))
                {
                    FileInfo sourceFile = new FileInfo(actionItem.Source);

                    length += sourceFile.Length;
                }
            }

            return length;
        }
    }
}
